require "Task"
require "TaskCategory/DailyTask"
require "WeightPool"

QNW_TS_DailyTaskBase = QNW_TS_Task:derive("QNW_TS_DailyTaskBase")

local this = QNW_TS_DailyTaskBase
this.taskCategory = QNW_TS_DailyTask

function QNW_TS_DailyTaskBase:isComplete()
    return self.count >= self.maxCount and self:isTime()
end

function QNW_TS_DailyTaskBase:isAbandon()
    return false
end

function QNW_TS_DailyTaskBase:getProgress()
    local count = math.min(self.count, self.maxCount)
    return tostring(count) .. "/" .. self.maxCount
end

function QNW_TS_DailyTaskBase:save()
    local save = {}
    save.Type = self.Type
    save.uuid = self.uuid
    save.count = self.count
    save.time = self.time
    save.rewards = {}
    for k, v in pairs(self.rewards) do
        table.insert(save.rewards, v:save())
    end
    return save
end

function QNW_TS_DailyTaskBase:load(_table)
    self.uuid = _table.uuid
    self.count = _table.count
    self.time = _table.time
    if _table.rewards then
        self:loadReward(_table.rewards)
    end
end

function QNW_TS_DailyTaskBase:registerClient(_player)
    _player:getModData().dailyTaskUpdateTime = getGameTime():getCalender():getTimeInMillis()
end

function QNW_TS_DailyTaskBase:new()
    local o = QNW_TS_Task:new()
    setmetatable(o, self)
    self.__index = self

    o.count = 0
    o.maxCount = 0
    o.uuid = getRandomUUID()
    local calender = getGameTime():getCalender()
    local time = calender:get(QNW_TS_Config.Calender.HOUR_OF_DAY) * 60 * 60 * 1000 + calender:get(QNW_TS_Config.Calender.MINUTE) * 60 * 1000 + calender:get(QNW_TS_Config.Calender.SECOND) * 1000 + calender:get(QNW_TS_Config.Calender.MILLISECOND)
    o.time = getGameTime():getCalender():getTimeInMillis() + 25 * 60 * 60 * 1000 - time

    return o
end